﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;

namespace BO
{
    partial class SysUser
    {
        public override bool IsDirty
        {
            get
            {

                return base.IsDirty || IsChildDirty(_ISysUserRoleList);
            }
        }

        public string CompanyFilter
        {
            get
            {
                if (AllowAllCompany||IsSystemUser)
                    return " 1=1 ";
                else
                    return string.Format(" (CompanyName = '{0}' OR CompanyName='')", CompanyName);
            }
        }

        private SysUserRoleList _ISysUserRoleList ;
        /// <summary>
        /// 可编辑的,用于分配角色.
        /// </summary>
        public SysUserRoleList ISysUserRoleList
        {
            get
            {
                if (_ISysUserRoleList == null)
                {
                    
                    _ISysUserRoleList = ListFactory<SysUserRoleList>.GetList(string.Format("UserName='{0}'", this.UserName));
                    _ISysUserRoleList.SetHeader(this);
                }
                return _ISysUserRoleList;
            }
        }
        public bool IsSystemUser
        {
            get
            {
                return 
                    UserName.Equals("System", StringComparison.CurrentCultureIgnoreCase) ||
                    UserName.Equals("SUPERUSER", StringComparison.CurrentCultureIgnoreCase);
            }
        }

       
        /// <summary>
        /// 该用户是否已分配角色.
        /// </summary>
        public bool HasNoRole
        {
            get
            {
                if (IsSystemUser)
                    return false;
                if (ISysUserRoleList.Count == 0)
                    return true;
                return false;
            }
        
        }

        private RoleAccessList _ReadRoleAccessList;
        /// <summary>
        /// 只用于获取当前用户的操作权限,不能添加和修改该集合.
        /// </summary>
        public RoleAccessList ReadRoleAccessList
        {
            get
            {
                if (IsSystemUser)
                    _ReadRoleAccessList = new RoleAccessList();

                else if (_ReadRoleAccessList == null)
                {
                        _ReadRoleAccessList = ListFactory<RoleAccessList>.GetList(string.Format("RoleName IN (Select SysUserRole.RoleName From SysUserRole Where UserName='{0}')", UserName));
                        _ReadRoleAccessList.AllowEdit = false;
                }
                    _ReadRoleAccessList.SetHeader(this);
              
                return _ReadRoleAccessList;
            }
        }
        protected override void UpdateChildren(List<DbCommand> cmdList)
        {
            if (_ISysUserRoleList == null)
                return;
            if (IsDeleted)
                _ISysUserRoleList.Clear();
            _ISysUserRoleList.Update(cmdList);
        }
    }
}
